<?php

class Ead_Form_Repositorio extends Zend_Form
{

    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
        
        $this->setName('form_'.$controller);
        
        $id = $front->getRequest()->getParam('id');
        
        $el = new Zend_Form_Element_Hidden('REP_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
                
        $obj = new Ead_Model_DbTable_Tipomaterial();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['TIP_ID']] = $r['TIP_NOM']; 
        
        $el = new Zend_Form_Element_Select('TIP_ID',array('onchange' => ''));
        $el->setLabel('Tipo de Material')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty')
        ->addMultiOptions($arr);
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REP_NOM');
        $el->setLabel('Nome')
                ->setRequired(true)
                ->setAttrib('class', 'find_rep')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REP_PRTNUM');
        $el->setLabel('Número da Parte')
                //->setRequired(true)
                ->setAttrib('class', 'numero')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('REP_PRTQTD');
        $el->setLabel('Quantidade de Partes')
                //->setRequired(true)
                ->setAttrib('class', 'numero')
                ->addFilter('StripTags')
                ->addFilter('StringTrim');
        $this->addElement($el);
       
        $el = new Zend_Form_Element_File('REP_FILE');
        $el->setLabel('Arquivo');
	//->addValidator('Extension', false, array('mp3', 'png', 'gif'));
	//->addValidator('Size', false, 10240000);
	//$el->setDestination('public/upload/minibanner');
        $this->addElement($el);  
        
        
        if($id > 0){
            Zend_Loader::loadClass('imagem','library');
            
            $obj = new Ead_Model_DbTable_Repositorio();
            $row = $obj->fetchRow('REP_ID = '.(int)$id);
            
            $arq = imagem::formatNomeArquivo($row->REP_ID,$row->REP_DATHOR);
 
            $img = 'public/upload/'.$controller.'/'.$arq.'.*';
            foreach (glob($img) as $src) {
                $ext = end(explode('.', $src));    
                if($ext == 'mp4'){
                    
                    $sessao = new Zend_Session_Namespace('Repositorio');
                    $sessao->caminho = '..'.$urlbase.'/public/upload/repositorio/';
                    $sessao->id = $row->REP_ID;
                    $sessao->dathor = $row->REP_DATHOR;
                    
                    Zend_Loader::loadClass('tools','library');
                    
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>tools::frameVideo()));
                    $this->addElement($el);
                }
                else{
                    $el = new Zend_Form_Element('img',array( 'helper'=>'formNote', 'value'=>'<img height="200" src="'.$front->getBaseUrl().'/'.$src.'" />' ));
                    $this->addElement($el);
                }
            }
        }
        
        $el = new Zend_Form_Element_Checkbox('REP_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
        
    }

}

